package com.gh.controller;

import com.gh.result.Result;
import com.gh.service.PaymentService;
import com.gh.service.WeixinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Author Eric
 * @Date 2021/6/28 20:35
 * @Version 1.0
 */
@RequestMapping("/api/order/weixin")
@RestController
public class WxPayController {
    @Autowired
    private WeixinService weixinService;
    @Autowired
    private PaymentService paymentService;

    /**
     * 生成微信支付二维码
     */
    @GetMapping("createNative/{orderNo}")
    public Result createNative(@PathVariable Long orderNo) {
        Map map = weixinService.createNative(orderNo);
        return Result.ok(map);
    }

    /**
     * 查询支付状态的方法
     */
    @GetMapping("queryPayStatus/{orderId}")
    public Result queryPayStatus(@PathVariable Long orderId) {
        Map<String, String> map = weixinService.queryPayStatus(orderId);
        if (map == null) {
            return Result.fail("支付出错了");
        }
        if ("SUCCESS".equals(map.get("trade_state"))) {
            //更新支付状态信息
            String out_trade_no = map.get("out_trade_no");//订单编码
            paymentService.paySuccess(out_trade_no, map);
            return Result.ok().message("支付成功");
        }
        return Result.ok().message("支付中");
    }
}
